<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vue.js 搜索页面</title>
    <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.min.js"></script>
    <style>
        /* 隐藏未编译的变量 */

        [v-cloak] {
            display: none;
        }

        * {
            margin: 0;
            padding: 0;
        }

        body {
            font: 15px/1.3 'Open Sans', sans-serif;
            color: #5e5b64;
            text-align: center;
        }

        a, a:visited {
            outline: none;
            color: #389dc1;
        }

        a:hover {
            text-decoration: none;
        }

        section, footer, header, aside, nav {
            display: block;
        }


        /*-------------------------
            搜索输入框
        --------------------------*/

        .bar {
            background-color: #5c9bb7;

            background-image: -webkit-linear-gradient(top, #5c9bb7, #5392ad);
            background-image: -moz-linear-gradient(top, #5c9bb7, #5392ad);
            background-image: linear-gradient(top, #5c9bb7, #5392ad);

            box-shadow: 0 1px 1px #ccc;
            border-radius: 2px;
            width: 400px;
            padding: 14px;
            margin: 45px auto 20px;
            position: relative;
        }

        .bar input {
            background: #fff no-repeat 13px 13px;
            background-image: url();

            border: none;
            width: 100%;
            line-height: 19px;
            padding: 11px 0;

            border-radius: 2px;
            box-shadow: 0 2px 8px #c4c4c4 inset;
            text-align: left;
            font-size: 14px;
            font-family: inherit;
            color: #738289;
            font-weight: bold;
            outline: none;
            text-indent: 40px;
        }

        ul {
            list-style: none;
            width: 428px;
            margin: 0 auto;
            text-align: left;
        }

        ul li {
            border-bottom: 1px solid #ddd;
            padding: 10px;
            overflow: hidden;
        }

        ul li img {
            width: 60px;
            height: 60px;
            float: left;
            border: none;
        }

        ul li p {
            margin-left: 75px;
            font-weight: bold;
            padding-top: 12px;
            color: #6e7a7f;
        }
    </style>
</head>
<body>

<form id="main" v-cloak>

    <div class="bar">
        <!-- searchString 模型与文本域创建绑定 -->

        <input type="text" v-model="searchString" placeholder="输入搜索内容"/>
    </div>

    <ul>
        <!-- 循环输出数据 -->
        <li v-for="article in filteredArticles">
            <a v-bind:href="article.url"><img v-bind:src="article.image"/></a>
            <p>{{article.title}}</p>
        </li>
    </ul>

</form>
<script>
    new Vue({
        el: '#main',
        data: {
            searchString: "",

            // 数据模型，实际环境你可以根据 Ajax 来获取

            articles: [
                {
                    "title": "【酒厂直发】贵州53度酱香型2.5L坛装纯粮坤沙白酒送礼摆件",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-GBH30",
                    "image": "https://bj.ddnkj.cn/jiu/DZ/l1.jpg"
                },
                {
                    "title": "【细腻回甘】贵州贵茅酒53度酱香型白酒纯粮食高粱大曲坤沙礼盒装",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-WZJ",
                    "image": "https://bj.ddnkj.cn/jiu/1935/1935-6/l2.jpg"
                },
                {
                    "title": "【限时大促】国酱1935，厂家直销，现在下单仅需1折，速抢！",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-1949",
                    "image": "https://bj.ddnkj.cn/jiu/1935/1935-6/l1.jpg"
                },
                {
                    "title": "【酒厂福利价】原浆1983 53°茅香，千元品质佳酿，没有一滴勾兑，最后500箱！赶紧抢",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-DZ",
                    "image": "https://bj.ddnkj.cn/jiu/DZ/l3.jpg"
                },
                {
                    "title": "【贵州王子酒】纯粮坤沙,千元品质佳酿,最后500箱!赶紧抢",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-GZGM/",
                    "image": "https://bj.ddnkj.cn/jiu/wzj/2/tou1.jpg"
                },
                {
                    "title": "Creating Your First Desktop App With HTML, JS and Electron",
                    "url": "https://tiao.ddnkj.cn/QSS/S/lm-1935/",
                    "image": "https://bj.ddnkj.cn/jiu/DZ/l2.jpg"
                }
            ]
        },
        computed: {
            // 计算数学，匹配搜索
            filteredArticles: function () {
                var articles_array = this.articles,
                    searchString = this.searchString;

                if (!searchString) {
                    return articles_array;
                }
                /*处理搜索字符串：将searchString去除首尾空格并转为小写*/
                searchString = searchString.trim().toLowerCase();
                /*过滤文章数组：使用filter方法遍历articles_array，通过检查每篇文章标题是否包含搜索关键词来筛选结果*/
                articles_array = articles_array.filter(function (item) {
                    /*匹配逻辑：将文章标题转为小写后，使用indexOf方法查找是否包含搜索词，如果找到则保留该文章*/
                    if (item.title.toLowerCase().indexOf(searchString) !== -1) {
                        return item;
                    }
                })

                // 返回过来后的数组
                return articles_array;
            }
        }
    });
</script>
</body>
</html>
